/**
 * SysRoleServiceImpl.java
 * Created at 2017-2-7
 * Created by shihui
 * Copyright (C) 2017 BROADTEXT SOFTWARE, All rights reserved.
 */
package com.broadtext.rms.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.broadtext.common.bean.EntityPageBean;
import com.broadtext.common.exp.DaoException;
import com.broadtext.common.exp.ServiceException;
import com.broadtext.common.repository.EntityDao;
import com.broadtext.common.service.impl.EntityServiceImpl;
import com.broadtext.common.utils.UUIDUtils;
import com.broadtext.rms.system.dao.IVsysUserDao;
import com.broadtext.rms.system.model.SysRole;
import com.broadtext.rms.system.model.SysUserRole;
import com.broadtext.rms.system.model.TrResRole;
import com.broadtext.rms.system.service.ISysRoleService;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;

/**
 * <p>ClassName: SysRoleServiceImpl</p>
 * <p>Description: 角色业务实现类</p>
 * <p>Author: shihui</p>
 * <p>Date: 2017-2-7</p>
 */
@Service("sysRoleService")
public class SysRoleServiceImpl extends EntityServiceImpl<SysRole> implements ISysRoleService {
    /**
     * 
     * <p>Description: 注入角色Dao</p>
     * @param entityDao 角色Dao
     */
    @Autowired
    @Qualifier("sysRoleDao")
    public void setEntityDao(EntityDao<SysRole> entityDao) {
        super.entityDao = entityDao;
    }

    /**
     * 注入用户(视图)Dao
     */
    @Autowired
    @Qualifier("vSysUserDao")
    private IVsysUserDao vSysUserDao;
    
    /**
     * 
     * <p>Description: 查询角色列表</p>
     * @return 角色
     * @throws DaoException dao异常
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<SysRole> getRolesTree() throws DaoException {
        return this.entityDao.findByQuery("com.broadtext.rms.system.model.SysRole.findEntityPageByQuery", null);
    }

    /**
     * 
     * <p>Description: 根据条件查询角色下的人员</p>
     * @param map 查询条件
     * @param pageBounds 翻页
     * @return 人员列表
     * @throws ServiceException 异常
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @Override
    public EntityPageBean<Object> findUserByRole(Map<String, Object> map, PageBounds pageBounds)
        throws ServiceException {
        EntityPageBean pb;
        pb = new EntityPageBean();
        try {
            if (map.containsKey("userType")) {
                pb = this.vSysUserDao.findUserPageByRole(map, pageBounds);
            } else {
                pb = this.vSysUserDao.findUserPageByRoleToAdd(map, pageBounds);
            }
        } catch (DaoException e) {
            throw new ServiceException(e);
        }
        return pb;
    }

    /**
     * 
     * <p>Description: 删除（伪）角色</p>
     * @param map 角色信息
     * @throws ServiceException 异常
     */
    @Override
    public void delRole(Map<String, Object> map) throws ServiceException {
        try {
            //删除角色
            this.entityDao.updateRecord("com.broadtext.rms.system.model.SysRole.deleteRoleById", map);
            //删除关联资源记录
            this.entityDao.deleteRecord("com.broadtext.rms.system.model.TrResRole.deleteByRoleId", map.get("roleId"));
            //删除关联用户记录
            this.entityDao.deleteRecord("com.broadtext.rms.system.model.TrUserRole.deleteByRoleId", map.get("roleId"));
        } catch (DaoException e) {
            throw new ServiceException(e);
        }
    }

    /**
     * 
     * <p>Description: 锁定角色</p>
     * @param map 角色信息
     * @throws ServiceException 异常
     */
    @Override
    public void lockRole(Map<String, Object> map) throws ServiceException {
        try {
            this.entityDao.updateRecord("com.broadtext.rms.system.model.SysRole.lockRoleById", map);
        } catch (DaoException e) {
            throw new ServiceException(e);
        }
    }

    /**
     * 
     * <p>Description: 向角色添加用户</p>
     * @param list 添加记录
     * @throws ServiceException 异常
     */
    @Override
    public void addUsersToRole(List<SysUserRole> list) throws ServiceException {
        for (SysUserRole r : list) {
            try {
                this.entityDao.insertRecord("com.broadtext.rms.system.model.SysUserRole.insertRecord", r);
            } catch (DaoException e) {
                throw new ServiceException(e);
            }
        }
    }

    /**
     * 
     * <p>Description: 删除角色用户</p>
     * @param map 角色和用户信息
     * @throws ServiceException 异常
     */
    @Override
    public void deleteUserFromRole(Map<String, Object> map) throws ServiceException {
        try {
            this.entityDao.updateRecord("com.broadtext.rms.system.model.SysRole.deleteUserFromRole", map);
        } catch (DaoException e) {
            throw new ServiceException(e);
        }
    }

    /**
     * 
     * <p>Description: 查询新增角色是否已存在</p>
     * @param role 角色实体
     * @return 查询结果
     * @throws ServiceException 异常
     */
    @SuppressWarnings("unchecked")
    @Override
    public SysRole findRoleExist(SysRole role) throws ServiceException {
        SysRole existRole = null;
        try {
            List<SysRole> rList;
            rList = this.entityDao.findByProperty("com.broadtext.rms.system.model.SysRole.findExistRole", role);
            if (rList.size() > 0) {
                existRole = rList.get(0);
            }
        } catch (DaoException e) {
            throw new ServiceException(e);
        }
        return existRole;
    }

    /**
     * 
     * <p>Description: 根据角色查询资源</p>
     * @param id 角色ID
     * @return 资源
     * @throws ServiceException 异常
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<String> findResourceByRole(String id) throws ServiceException {
        List<String> resList;
        resList = new ArrayList<String>();
        try {
            resList = this.entityDao.findByProperty(
                "com.broadtext.rms.system.model.SysResource.findResByRole", id);
        } catch (DaoException e) {
            throw new ServiceException(e);
        }
        return resList;
    }

    /**
     * 
     * <p>Description: 保存角色资源</p>
     * @param roleId 角色ID
     * @param res 资源
     * @return 操作结果
     * @throws ServiceException 异常
     */
    @Override
    public void saveRoleResource(String roleId, String[] res) throws ServiceException {
        try {
            //删除角色之前的资源数据
            this.entityDao.deleteRecord("com.broadtext.rms.system.model.TrResRole.deleteByRoleId", roleId);
            TrResRole resRole;
            //保存新数据
            for (String resId : res) {
                resRole = new TrResRole();
                resRole.setPkId(UUIDUtils.getUUID());
                resRole.setResId(resId);
                resRole.setRoleId(roleId);
                this.entityDao.insertRecord("com.broadtext.rms.system.model.TrResRole.insertRecord", resRole);
            }
        } catch (DaoException e) {
            throw new ServiceException(e);
        }
    }
    
}
